package com.ruoyi.monitor.controller;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.monitor.domain.AutoSendTask;
import com.ruoyi.monitor.service.IAutoSendTaskService;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 定时子任务Controller
 * 
 * @author xzx
 * @date 2023-03-31
 */
@RestController
@RequestMapping("/system/task")
public class AutoSendTaskController extends BaseController
{
    @Autowired
    private IAutoSendTaskService autoSendTaskService;
    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时子任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(AutoSendTask autoSendTask)
    {
        startPage();
        List<AutoSendTask> list = autoSendTaskService.selectAutoSendTaskList(autoSendTask);
        return getDataTable(list);
    }

    /**
     * 导出定时子任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "定时子任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AutoSendTask autoSendTask)
    {
        List<AutoSendTask> list = autoSendTaskService.selectAutoSendTaskList(autoSendTask);
        ExcelUtil<AutoSendTask> util = new ExcelUtil<AutoSendTask>(AutoSendTask.class);
        util.exportExcel(response, list, "定时子任务数据");
    }

    /**
     * 获取定时子任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(autoSendTaskService.selectAutoSendTaskById(id));
    }

    /**
     * 新增定时子任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "定时子任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AutoSendTask autoSendTask)
    {
        return toAjax(autoSendTaskService.insertAutoSendTask(autoSendTask));
    }

    /**
     * 修改定时子任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "定时子任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AutoSendTask autoSendTask)
    {
        return toAjax(autoSendTaskService.updateAutoSendTask(autoSendTask));
    }

    /**
     * 删除定时子任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "定时子任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        list.forEach(id->{
            AutoSendTask autoSendTask = autoSendTaskService.selectAutoSendTaskById(id);
            List<AutoSendTask> autoSendTasks = autoSendTaskService.selectTasksByJobId(autoSendTask.getJobId());
            if (autoSendTasks.size()==1){
                SysJob job = new SysJob();
                job.setJobId(autoSendTask.getJobId());
                try {
                    jobService.deleteJob(job);
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
            autoSendTaskService.deleteAutoSendTaskById(id);
        });
        return toAjax(ids.length);
    }
}
